package com.eneat.smartrule.controller;

import com.eneat.smartrule.restAPI.AbstractController;
import com.eneat.smartrule.restAPI.HttpResult;
import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(path = "/v1/condition")
@Tag(name = "规则引擎相关接口")
@Slf4j
@CrossOrigin
public class ConditionController extends AbstractController {
    @Operation(summary = "测试")
    @GetMapping(path = "/test")
    public HttpResult<String> test() {
        String expression = "a=='河北省'&&((b=='男人'||c>=30)||d=='黄')";

        // 编译表达式
        Expression compiledExp = AviatorEvaluator.compile(expression);
       //test
        Map<String, Object> env = new HashMap<>();
        env.put("a", "河北省");
        env.put("b", "男人");
        env.put("c",1);
        env.put("d","黄");


        // 执行表达式
        System.out.println( compiledExp.execute(env));
        return new HttpResult<>("success", true, 200, "success");

    }
}
